<?php
/*
 * Created on 18-sept.-2006
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class SequencesController extends Zend_Controller_Action
 {
    public function indexAction()
    {    	
        $get = Zend::registry('fGet'); 
        $session = Zend::registry('fSession');
        $db = Zend::registry('db');
        $acl = Zend::registry('acl');   

        $mySelectedThread = $get->getRaw('threadId');
        $mySelectedDate = $get->getRaw('selectedDate');
        $mySelectedType = $get->getRaw('selectedType');
        $mySelectedListe = $get->getRaw('selectedListe');

        // Fixation du nombre de colonnes a afficher //
        if ( $mySelectedThread > 0 )
        {
            $myCols = 10;
        } else 
        {
            $myCols = 4;
        }

        // Selection des differentes camera ou de la camera concernée //
        $sql = "SELECT * FROM threads ";
        if ( $mySelectedThread > 0 )
        {
            $sql .= "WHERE id = '" . $mySelectedThread . "'";
        }
        $myThreads = $db->fetchAll($sql);

        // Selection des differentes dates d'enregistrement relative ou non a une camera //
        $sql = "SELECT distinct(date) FROM security WHERE file_type = '8'";
        if ( $mySelectedThread > 0 )
        {
            $sql .= "WHERE camera = '" . $mySelectedThread . "' ";
        }
        if ( $mySelectedDate > "" )
        {
            if ( $mySelectedThread > 0 )
            {
                $sql .= "AND date = '" . $mySelectedDate . "' ";
            } else
            {
        	    $sql .= "WHERE date = '" . $mySelectedDate . "' ";
            }
        }
        $sql .= "ORDER BY time_stamp DESC";
        $myDatesSelection = $db->fetchAll($sql);


        // Pour chaque date //  
        $tblParDate = array();
        foreach ( $myDatesSelection as $myDate )
        {
            $mySequencesDate = $myDate['date'];
            
            // Pour chaque camera // 
            $tblParThread = array();
            foreach ( $myThreads as $myThread )
            {
            	$mySequencesThreadId = $myThread['id'];
                $mySequencesThreadName = $myThread['name'];
                
                $sql = "SELECT * FROM security WHERE file_type = '8' AND camera = '" . $mySequencesThreadId . "' ";
                switch ($mySelectedType){
                    case "lu" : $sql .= "AND lu = 'O' "; break;                    
                    case "nonlu" : $sql .= "AND lu = 'N' "; break;
                }                
                $sql .= "AND date = '" . $mySequencesDate . "' ORDER BY time_stamp DESC";
                $mySequencesSelection = $db->fetchAll($sql); 

                $tblSequences = array();                               
                foreach ( $mySequencesSelection as $mySequence )
                {
                    $mySequenceHeure = $mySequence['heure'];
                    $mySequenceTextEvent = $mySequence['text_event'];                    
                    $mySequenceFile = C_MOTION_DIR . $mySequencesThreadName . '/' . basename($mySequence['filename']);
                    $mySequenceFileSize = round(filesize($mySequenceFile)/1024,1);
            
                    $sql = "SELECT filename FROM security WHERE file_type = '1' AND camera = '" . $mySequencesThreadId . "' " .
                            "AND text_event = '" . $mySequenceTextEvent . "'";
                    //$mySequenceImage = C_MOTION_DIR . $mySequencesThreadName . '/' . basename($db->fetchOne($sql));
                    $mySequenceImage = photo_mini(C_MOTION_DIR . $mySequencesThreadName . '/' . basename($db->fetchOne($sql)));
                    
                    array_push($tblSequences, array("heure"=>$mySequenceHeure, "img"=>$mySequenceImage, "video"=>$mySequenceFile, "size"=>$mySequenceFileSize));
                }
                array_push($tblParThread, array("id"=>$mySequencesThreadId, "data"=>$tblSequences));                
            }
            array_push($tblParDate, array("date"=>$mySequencesDate, "data"=>$tblParThread));
        }                


        $myAllowed = $acl->isAllowed($session->groupe, null, 'delete');


        $view = Zend::registry('view');
        $view->cols = $myCols;
        $view->selectedThread = $mySelectedThread;
        $view->selectedDate = $mySelectedDate;
        $view->selectedType = $mySelectedType;
        $view->selectedListe = $mySelectedListe;
        $view->threads = $myThreads;
        $view->userAllowed = $myAllowed;
                
        $view->sequencesParDate = $tblParDate;
        if ( $mySelectedListe == 'liste' ) $mySequencesListe = $view->render('sequencesListeView.php');        
            else $mySequencesListe = $view->render('sequencesTableauView.php');        
        
        $view->sequencesListe = $mySequencesListe;
        echo $view->render('sequencesView.php');   
    }

    public function listerAction()
    {
        
    }
           
    public function consultAction()
    {      
        $get = Zend::registry('fGet');
        $session = Zend::registry('fSession');
        $db = Zend::registry('db');        
        $acl = Zend::registry('acl');  
    
        $myThreadId = $get->getRaw('threadId');
        $mySelectedThread = $get->getRaw('selectedThread');
        $mySelectedType = $get->getRaw('selectedType');
        $myVideoFile = $get->getRaw('file');
        $mySelectedDate = $get->getRaw('selectedDate');    
        $mySelectedListe = $get->getRaw('selectedListe');  
    
        // PREPARATION DU FICHIER VIDEO A LIRE SUR LE FLVPLAYER //
        exec('ffmpeg -i ' . $myVideoFile . ' -s 320x240 -r 5 -f flv -y ' . C_MOTION_DIR .'current.flv');
        $myFlvFile = '/' . C_MOTION_DIR .'current.flv';
        
        // MARQUAGE DE LA SEQUENCE COMME LU //
        $sql = "UPDATE security SET lu = 'O' " .
                "WHERE file_type = '8' AND filename LIKE '%" . $myVideoFile . "'";
        $db->query($sql);
        
        $sql = "SELECT * FROM security, threads WHERE camera = id AND filename LIKE '%" . $myVideoFile . "%'";
        $mySequence = $db->fetchRow($sql);
    
        $myAllowed = $acl->isAllowed($session->groupe, null, 'delete');
    
        // PREPARATION DE LA PAGE D'AFFICHAGE DES SEQUENCES //
        $view = Zend::registry('view');
        $view->myFlvFile = $myFlvFile;
        $view->myAviFile = $myVideoFile;
        $view->threadId = $myThreadId;
        $view->threadName = $mySequence['name'];
        $view->sequenceDate = $mySequence['date'];
        $view->sequenceHeure = $mySequence['heure'];
    
        $view->selectedDate = $mySelectedDate;
        $view->selectedThread = $mySelectedThread;
        $view->selectedType = $mySelectedType;
        $view->selectedListe = $mySelectedListe;
    
        $view->userAllowed = $myAllowed;
        echo $view->render('sequencesConsultView.php');
    }
        
    public function deleteAction()
    {
        $get = Zend::registry('fGet');         
        $session = Zend::registry('fSession');
        $db = Zend::registry('db');
        $acl = Zend::registry('acl');             

        $myThreadId = $get->getRaw('threadId');
        $myVideoFile = $get->getRaw('file');
        $mySelectedThread = $get->getRaw('selectedThread');
        $mySelectedDate = $get->getRaw('selectedDate');
        $mySelectedType = $get->getRaw('selectedType');  
        $mySelectedListe = $get->getRaw('selectedListe');      

        if ( $acl->isAllowed($session->groupe, null, 'delete') ){        
            $sql = "SELECT text_event FROM security WHERE camera = '" . $myThreadId . "' AND filename LIKE '%" . $myVideoFile . "'";
            $myTextEvent = $db->fetchOne($sql);                                
            
            $sql = "SELECT * FROM security WHERE camera = '" . $myThreadId . "' AND text_event = '" . $myTextEvent . "'";
            $myFiles = $db->fetchAll($sql);        
            
            foreach ( $myFiles as $myFile )
            {
                $myMiniFile = C_MOTION_DIR."photo_".md5(strstr($myFile['filename'], C_MOTION_DIR)).".png";
                if ( file_exists( $myFile['filename'] ) ){               
                    unlink( $myFile['filename'] );
                    if ( file_exists($myMiniFile)) unlink($myMiniFile);                
                }
            }
            
            $sql = "DELETE FROM security WHERE camera = '" . $myThreadId . "' AND text_event = '" . $myTextEvent . "'";
            $db->query($sql);
        }

        $this->_redirect('/sequences?threadId=' . $mySelectedThread . '&selectedType=' . $mySelectedType . '&selectedDate=' . $mySelectedDate . '&selectedListe=' . $mySelectedListe);        
    }
    
    public function selectionAction()
    {
        $post = Zend::registry('fPost');
        $session = Zend::registry('fSession');
        $db = Zend::registry('db');
        $acl = Zend::registry('acl');  

        $mySelectedThread = $post->getRaw('threadId');
        $mySelectedDate = $post->getRaw('selectedDate');
        $mySelectedType = $post->getRaw('selectedType');
        $myFileCount = $post->getRaw('fileCount');
        $mySelectedListe = $post->getRaw('selectedListe');        
        $myAction = $post->getRaw('action');

        if ( $acl->isAllowed($session->groupe, null, 'delete') ){ 
            for ($i = 0; $i < $myFileCount; $i++){
                $myFile = $post->getRaw('checkbox'.$i);
    
                switch ( $myAction ) {
                    case "Archiver":  
                        if ( $myFile > "" )
                        {
                            $sql = "UPDATE security SET lu = 'O' " .
                                    "WHERE file_type = '8' AND filename LIKE '%" . $myFile . "'";
                            $db->query($sql);
                        }                    
                    break;
    
                    case "Desarchiver":
                        if ( $myFile > "" )
                        {
                            $sql = "UPDATE security SET lu = 'N' " .
                                    "WHERE file_type = '8' AND filename LIKE '%" . $myFile . "'";
                            $db->query($sql);
                        }        
                    break;
    
                    case "Supprimer":                        
                        if ( $myFile > "" )
                        {
                            $sql = "SELECT text_event FROM security WHERE filename LIKE '%" . $myFile . "'";
                            $myTextEvent = $db->fetchOne($sql);
                            
                            $sql = "SELECT * FROM security WHERE text_event = '" . $myTextEvent . "'";
                            $myDeletedFiles = $db->fetchAll($sql);      
                            
                            foreach ( $myDeletedFiles as $myDeletedFile ){
                                if ( file_exists( $myDeletedFile['filename'] ) ) {
                                    $myMiniFile = C_MOTION_DIR."photo_".md5(strstr($myDeletedFile['filename'], C_MOTION_DIR)).".png";                                
                                    unlink( $myDeletedFile['filename'] );  
                                    if ( file_exists($myMiniFile) ) unlink($myMiniFile);                    
                                }
                            }
                            $sql = "DELETE FROM security WHERE text_event = '" . $myTextEvent . "'";
                            $db->query($sql); 
                        }
                    break;
                }
            }            
        }  
        $this->_redirect('/sequences?threadId=' . $mySelectedThread . '&selectedType=' . $mySelectedType . '&selectedDate=' . $mySelectedDate . '&selectedListe=' . $mySelectedListe);      
    }
    
    public function noRouteAction()
    {
        $this->_redirect('/');
    }
}
?>